#!/usr/bin/env python
# coding=utf-8

"""
@author: yaqiwe
@contact: 15338230927@163.com
@file: RequestUtil.py
@date: 2020/10/28 22:10
@desc: 网络请求的工具类
"""

import requests

from util.LogginUtil import logging


class RequestUtil:
    userAgent = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
    }

    TIME_OUT = 20
    STATUS_CODE = 200

    def getRequest(self, url, proxies):
        """
        使用代理ip访问
        :param url: url
        :param proxies: 代理网址
        :return:
        """
        try:
            res = requests.get(url, headers=self.userAgent, proxies=proxies, timeout=self.TIME_OUT)
            if res.status_code == self.STATUS_CODE:
                return res
            else:
                logging.debug('请求错误,statusCode:{}'.format(res.status_code))
        except Exception as e:
            logging.error('代理url访问错误：{}  '.format(e))
        return -1

    def getRequestThisIp(self, url):
        """
        使用本机ip访问
        :param url:
        :return:
        """
        res = requests.get(url, headers=self.userAgent, timeout=self.TIME_OUT)
        return res
